package com.moleq.service;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.moleq.db.DBError;
import com.moleq.db.DatabaseHelper;
import com.moleq.db.DatabaseSchema;
import com.moleq.dialog.MessageDialog;
import com.moleq.dialog.MessageDialog.OnClickMessageListener;
import com.moleq.models.DeptSalesRpt;
import com.moleq.models.PluSalesRpt;

public class DeptSalesRptService {
	private DatabaseHelper DBHelper;
	private MessageDialog mMessageDialog;
	Context context;

	public DeptSalesRptService() {
	}

	public DeptSalesRptService(Context context) {
		this.context = context;
		this.DBHelper = new DatabaseHelper(context);
	}

	public void DeleteAll() {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL("DELETE FROM " + DatabaseSchema.DeptSalesRpt.SCHEMA_NAME);
	}

	public void DeleteByID(long id) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL("DELETE FROM " + DatabaseSchema.DeptSalesRpt.SCHEMA_NAME
				+ " WHERE " + DatabaseSchema.DeptSalesRpt.DS_ID + " = ?",
				new Object[] { id });
	}

	public void save(DeptSalesRpt obj) {
		if (CheckDepInList(obj.getDs_dept(), obj.getDs_date(), obj.getDs_type())) {
			this.Update(obj);
		} else {
			this.Insert(obj);
		}
	}

	public boolean CheckDepInList(double dep, String date, String type) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();

		Cursor cursor = db.rawQuery("select ds_dept from "
				+ DatabaseSchema.DeptSalesRpt.SCHEMA_NAME + " where ds_dept = "
				+ dep + " and ds_date = '" + date + "' and ds_type = '" + type
				+ "'", null);
		if (cursor == null) {
			ShowMessage(DBError.error, mMessageDialog.MESSAGE_BOX_OK,
					mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return false;
		}
		if (cursor.moveToNext()) {
			cursor.close();
			return true;
		}
		cursor.close();
		return false;
	}

	public void Update(DeptSalesRpt obj) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL(
				"Update "
						+ DatabaseSchema.DeptSalesRpt.SCHEMA_NAME
						+ " set ds_date = ?,ds_qty = ds_qty + ?,"
						+ "ds_amt = ds_amt + ?,update_datetime = strftime('%Y%m%d%H%M%S','now'),ds_type = ?"
						+ " where ds_dept = ? and ds_date = ?",
				new Object[] { obj.getDs_date(), obj.getDs_qty(),
						obj.getDs_amt(), obj.getDs_type(), obj.getDs_dept(),
						obj.getDs_date() });
	}

	public List<DeptSalesRpt> FindAll() {
		List<DeptSalesRpt> obj = new ArrayList<DeptSalesRpt>();
		SQLiteDatabase db = DBHelper.getWritableDatabase();

		Cursor cursor = db.rawQuery("select * from "
				+ DatabaseSchema.DeptSalesRpt.SCHEMA_NAME, null);
		if (cursor == null) {
			ShowMessage(DBError.error, mMessageDialog.MESSAGE_BOX_OK,
					mMessageDialog.ICON_ERROR);
			DBError.error = "";
			return obj;
		}
		while (cursor.moveToNext()) {
			int ds_id = cursor.getInt(cursor
					.getColumnIndex(DatabaseSchema.DeptSalesRpt.DS_ID));
			int ds_store_id = cursor.getInt(cursor
					.getColumnIndex(DatabaseSchema.DeptSalesRpt.DS_STORE_ID));
			int ds_station = cursor.getInt(cursor
					.getColumnIndex(DatabaseSchema.DeptSalesRpt.DS_STATION));
			String ds_date = cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.DeptSalesRpt.DS_DATE));
			double ds_dept = cursor.getDouble(cursor
					.getColumnIndex(DatabaseSchema.DeptSalesRpt.DS_DEPT));
			int ds_qty = cursor.getInt(cursor
					.getColumnIndex(DatabaseSchema.DeptSalesRpt.DS_QTY));
			double ds_amt = cursor.getDouble(cursor
					.getColumnIndex(DatabaseSchema.DeptSalesRpt.DS_AMT));
			String ds_type = cursor.getString(cursor
					.getColumnIndex(DatabaseSchema.DeptSalesRpt.DS_TYPE));

			obj.add(new DeptSalesRpt(ds_id, ds_store_id, ds_station, ds_date,
					ds_dept, ds_qty, ds_amt, ds_type));
		}
		cursor.close();
		return obj;
	}

	public List<DeptSalesRpt> getDeptSalesRptsByDate(String Date) {
		List<DeptSalesRpt> list = new ArrayList<DeptSalesRpt>();
		String sql = "select ds_id,ds_store_id,ds_station,ds_date,ds_dept,sum(ds_qty),sum(ds_amt),create_datetime,update_datetime,ds_type from t_dept_sales_rpt where ds_date = '"
				+ Date
				+ "' group by ds_date,ds_dept,ds_store_id,ds_station order by ds_dept";
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor == null)
			return list;
		while (cursor.moveToNext()) {
			DeptSalesRpt item = new DeptSalesRpt();
			item.setDs_id(cursor.getInt(0));
			item.setDs_store_id(cursor.getInt(1));
			item.setDs_station(cursor.getInt(2));
			item.setDs_date(cursor.getString(3));
			item.setDs_dept(cursor.getInt(4));
			item.setDs_qty(cursor.getInt(5));
			item.setDs_amt(cursor.getDouble(6));
			item.setDs_type(cursor.getString(9));

			list.add(item);
		}
		cursor.close();
		return list;
	}

	public String getDeptName(String DepId) {
		String sql = "select dep_name from t_department_setting where F03 = "
				+ DepId;
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor == null)
			return "";
		if (cursor.moveToNext()) {
			String name = cursor.getString(0);
			cursor.close();
			return name;
		}
		return "";
	}

	public void Insert(DeptSalesRpt obj) {
		SQLiteDatabase db = DBHelper.getWritableDatabase();
		db.execSQL(
				"INSERT INTO "
						+ DatabaseSchema.DeptSalesRpt.SCHEMA_NAME
						+ " VALUES(?,?,?,?,?,?,?,"
						+ "strftime('%Y%m%d%H%M%S','now'),strftime('%Y%m%d%H%M%S','now'),?)",
				new Object[] { null, obj.getDs_store_id(), obj.getDs_station(),
						obj.getDs_date(), obj.getDs_dept(), obj.getDs_qty(),
						obj.getDs_amt(), obj.getDs_type() });
	}

	public void ShowMessage(String text, int type, int icon) {
		mMessageDialog = new MessageDialog(context);
		mMessageDialog.setStyle(type);
		mMessageDialog.setMessageIcon(icon);
		mMessageDialog.setMessage(text);
		mMessageDialog.setOnClickMessageListener(onClickMessageListener);
		mMessageDialog.show();
	}

	private OnClickMessageListener onClickMessageListener = new OnClickMessageListener() {

		@Override
		public void onClickYes() {
			// TODO Auto-generated method stub

		}

		@Override
		public void onClickNo() {
			// TODO Auto-generated method stub

		}

		@Override
		public void onClickOK() {
			// TODO Auto-generated method stub
			mMessageDialog.dismiss();
		}
	};
}
